package com.wzx.ptplatform.service.impl;

import com.wzx.ptplatform.dao.PartTimeMessageDao;
import com.wzx.ptplatform.domain.PartTimeMessage;
import com.wzx.ptplatform.domain.WorkDetail;
import com.wzx.ptplatform.service.PartTimeMessageService;
import com.wzx.ptplatform.utils.PtplatformException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @author 紫煦桉
 */
@Service
public class PartTimeMessageServiceImpl implements PartTimeMessageService {

    @Resource
    private PartTimeMessageDao partTimeMessageDao;

    @Override
    public List<PartTimeMessage> findAll() throws PtplatformException {
        return partTimeMessageDao.findAll();
    }

    @Override
    public Page<PartTimeMessage> findList(PartTimeMessage partTimeMessage, Integer page, Integer limit) {
        Specification<PartTimeMessage> spec = (Specification<PartTimeMessage>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (partTimeMessage.getCompany() != null && partTimeMessage.getCompany().getCompanyName() != null) {
                exList.add(criteriaBuilder.like(root.get("company").get("companyName").as(String.class), "%" + partTimeMessage.getCompany().getCompanyName() + "%"));
            }
            if (partTimeMessage.getJob() != null && partTimeMessage.getJob().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("job").get("id").as(Long.class), partTimeMessage.getJob().getId()));
            }
            if (partTimeMessage.getCity() != null && partTimeMessage.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), partTimeMessage.getCity().getId()));
            }
            exList.add(criteriaBuilder.equal(root.get("postStatus").as(String.class), "1"));
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return partTimeMessageDao.findAll(spec, pageable);
    }

    @Override
    public PartTimeMessage findById(long id) throws PtplatformException {
        return partTimeMessageDao.findById(id).orElse(null);
    }

    @Override
    public boolean saveOrEdit(PartTimeMessage partTimeMessage) throws PtplatformException {
        try {
            partTimeMessageDao.saveAndFlush(partTimeMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteById(long id) throws PtplatformException {
        try {
            partTimeMessageDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Page<PartTimeMessage> findUnList(PartTimeMessage partTimeMessage, Integer page, Integer limit) throws PtplatformException {
        Specification<PartTimeMessage> spec = (Specification<PartTimeMessage>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (partTimeMessage.getCompany() != null && partTimeMessage.getCompany().getCompanyName() != null) {
                exList.add(criteriaBuilder.like(root.get("company").get("companyName").as(String.class), "%" + partTimeMessage.getCompany().getCompanyName() + "%"));
            }
            if (partTimeMessage.getJob() != null && partTimeMessage.getJob().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("job").get("id").as(Long.class), partTimeMessage.getJob().getId()));
            }
            if (partTimeMessage.getCity() != null && partTimeMessage.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), partTimeMessage.getCity().getId()));
            }
            exList.add(criteriaBuilder.equal(root.get("postStatus").as(String.class), "0"));
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return partTimeMessageDao.findAll(spec, pageable);
    }

    @Override
    public List<PartTimeMessage> findByCompanyId(Long id, String postStatus) throws PtplatformException {
        return partTimeMessageDao.findByCompanyIdAndPostStatus(id, postStatus);
    }

    @Override
    public List<PartTimeMessage> findByPostStatus(String postStatus) throws PtplatformException {
        return partTimeMessageDao.findByPostStatus(postStatus);
    }
}
